package nowcoder.offer;


/**
 * 字符流中第一个不重复的字符串
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-08-29 10:20
 */
public class FirstAppearingOnce {

    private StringBuilder stream = new StringBuilder();
    private int[] hash = new int[256];
    int i = 0;
    //Insert one char from stringstream
    public void insert(char ch)
    {
        hash[ch]++;
        stream.append(ch);

    }
    //return the first appearence once char in current stringstream
    public char firstAppearingOnce()
    {
        char[] chars = stream.toString().toCharArray();
        while (i < chars.length){
            if (hash[chars[i]] == 1)
                return chars[i];
            i++;
        }

        return '#';
    }
}
